package messagesvc.executor.template;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.ucloud.microsf.commonlib.response.PageListResponse;
import cn.ucloud.microsf.commonlib.util.PageUtil;
import cn.ucloud.microsf.messagesvc.orm.entity.TemplateEntity;
import cn.ucloud.microsf.messagesvc.orm.mapper.TemplateMapper;
import cn.ucloud.microsf.messagesvc.request.template.TemplateListRequest;
import cn.ucloud.microsf.messagesvc.response.template.TemplateResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息模板
 */
@Slf4j
@Service
public class TemplateListExecutor {

  @Autowired
  private TemplateMapper templateMapper;

  public PageListResponse<TemplateResponse> execute(TemplateListRequest request) {

    QueryWrapper<TemplateEntity> wrapper = new QueryWrapper<>();

    IPage<TemplateEntity> iPage = new Page<>(PageUtil.getPage(request.getPage()), PageUtil.getPageSize(request.getPageSize()));

    templateMapper.selectPage(iPage, wrapper);

    List<TemplateResponse> result = new ArrayList<>();

    if (CollectionUtil.isNotEmpty(iPage.getRecords())) {
      for (TemplateEntity record : iPage.getRecords()) {
        TemplateResponse response = new TemplateResponse();
        BeanUtil.copyProperties(record, response);
        result.add(response);
      }
    }

    return PageListResponse.generateResponse(result, (int) iPage.getPages(), (int) iPage.getSize(), (int) iPage.getTotal());

  }

}

